<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\Install;
use app\admin\model\getdatas\MySubject;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Beifen extends BaseController
{
    public function show_list(){
        return Builder::table()
            ->setAction('beifen/page')
            ->addToolbarDemo('alldel','全删除','','beifen/del')
            ->addToolbarDemo('add','备份','','beifen/show_add')
            ->setColumn('input','name','名称')
            ->setColumn('input','path','文件地址')
            ->setColumn('input','remarks','备注')
            ->setColumn('input','create_time','创建时间')
            ->setRightHeader()
            ->setButton('del','还原','beifen/huanyuan','','background-color: #01AAED')
            ->setButton('del','删除','beifen/del')
            ->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param();
        $data = Db::name(getTable('databases'))
            ->order('create_time desc')
            ->paginate($limit)->toArray();
        foreach ($data['data'] as $key=>$value){
            $value['create_time'] = date('Y-m-d H:i',$value['create_time']);
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }
    public function show_add(){
        return Builder::form()
            ->setAction('beifen/add')
            ->setProject('input','name','备份名称')
            ->setProject('textarea','remarks','备注')
            ->setButton('submit')
            ->setButton('back')
            ->fetch();
    }
    public function add(Request $request){
        $data = $request->param();
        $newdata['name']  = $data['name'];
        $newdata['remarks']  = $data['remarks'];
        if(MySubject::BeiFen($newdata)){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }
    public function huanyuan(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('databases'))
            ->where('id',$id)
            ->find();
        $obj = new Install();
        if($obj->huanyuan('.'.$data['path'])){
            if(is_file('.'.$data['path'])){
                unlink('.'.$data['path']);
            }
            return json_to_respos('还原成功',200);
        }else{
            return json_to_respos('还原失败',205);
        }

    }
    public function del(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('databases'))
            ->where('id','in',$id)
            ->select();
        foreach ($data as $value){
            if(is_file('./Data/'.$value['name'])){
                unlink('./Data/'.$value['name']);
            }
        }
        if(Db::name(getTable('databases'))->where('id','in',$id)->delete()){
            return json_to_respos('删除成功',200);
        }else{
            return json_to_respos('删除失败',205);
        }
    }
    public function zidongbeifen(){
        $data = Db::name(getTable('database_system'))
            ->order('id asc')
            ->select();
        if($data[0]['value']==0){
            return json('没有开启自动备份!');
        }
        if(time()<($data[2]['value']+$data[1]['value']*86400)){
            return json('没到自动备份时间!!');
        }
        $newdata['name'] = '自动备份';
        $newdata['remarks'] = date('Y-m-d H:i:s',time());
        if(MySubject::BeiFen($newdata)){
            return json('备份成功');
        }else{
            return json('备份失败');
        }
    }
}